# -*- coding: utf-8 -*-
'''
Created on 18-09-2012

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import simplejson as json
from CrawlerLib import commonlib
import pickle


max_page = 11

def getpage(page):
    print '> get page {0}'.format(page)
    url = 'http://a.p.mana.vn/video/dsvideo?lft=154&root=20&page={0}&page_size=30&imei=352172050420604&width=240&height=320&client=1.0.64&agency=TGDD'.format(page)
    html = commonlib.getHtml(url)
    data = json.loads(html)
    return data['data']

    
if __name__ == '__main__':
    
    cartoolList = None
    f = None
    try:
        f = open('cartoon.list', 'r')
        cartoolList = pickle.load(f)
    except:
        if f != None: f.close()
        f = open('cartoon.list', 'w')
    if cartoolList == None:
        cartoolList = []
        for page in range(1,12):
            jspage = getpage(page)
            for i in jspage:
                cartoolList.append(i)
        pickle.dump(cartoolList, f)
    f.close()
    
    print len(cartoolList)
    
    
    
    
    
    
    
    